package com.ruoyi.business.protocol.ask;

import ask.RtaRequestOuterClass;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.util.DigestUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

public class SimpleTestMain {
    public static void main(String[] args) throws Exception {

        sendProtobufSend();
//        baiduProtibufTest();
    }

    public static void baiduProtibufTest() {
        RtaApiRequestOuterClass.RtaApiRequest.Builder builder = RtaApiRequestOuterClass.RtaApiRequest.newBuilder();
        builder.setQid(123123123).setOsType(RtaApiRequestOuterClass.OsType.ANDROID).setDeviceIdMd5("12312").setSignTime(123123123).setToken("111");
        RtaApiRequestOuterClass.RtaApiRequest build = builder.build();
        //转换成字节数组
        byte[] s = build.toByteArray();

        RtaApiRequestOuterClass.RtaApiRequest demo1 = null;
        String jsonObject = null;
        try {
            //反序列化
            demo1 = RtaApiRequestOuterClass.RtaApiRequest.parseFrom(s);
            //转换成json
            jsonObject = JsonFormat.printer().print(demo1);
            System.out.println(jsonObject);
            String name = demo1.getOsType().getValueDescriptor().getName();
            System.out.println(name);
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }


    }

    public void protobufTest(){
        RtaRequestOuterClass.RtaRequest.Builder builder = RtaRequestOuterClass.RtaRequest.newBuilder();
        builder.setChannel("001")
                .setRequestId("1234567890")
                .setRequestTime(System.currentTimeMillis());

        RtaRequestOuterClass.RtaRequest build= builder.build();
        //转换成字节数组
        byte[] s = build.toByteArray();
        System.out.println("protobuf数据bytes[]:" + Arrays.toString(s));
        System.out.println("protobuf序列化大小: " + s.length);


        RtaRequestOuterClass.RtaRequest demo1 = null;
        String jsonObject = null;
        try {
            //反序列化
            demo1 = RtaRequestOuterClass.RtaRequest.parseFrom(s);

            //转 json
            jsonObject = JsonFormat.printer().print(demo1);

        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }

        System.out.println("Json格式化结果:\n" + jsonObject);
        System.out.println("Json格式化数据大小: " + jsonObject.getBytes().length);
    }

    public static void sendProtobufSend() throws IOException {
//        String url = "https://material-api.test.gifshow.com/rest/n/rta/common/kfy";  // 测试环境
//        String requestId = "00000298d27e48be9d4bb28574sgfq7b";  // 请求Id
//        String channel = "QUANNENG";  // 渠道标识，需向快手技术申请
//        String token = "Abcdefg";  // 渠道标识令牌


        String url = "https://promotion-partner.kuaishou.com/rest/n/rta/common/kfy";  // 测试环境
        String requestId = "00000298d27e48be9d9874564657b";  // 请求Id
        String channel = "HBHY";  // 渠道标识，需向快手技术申请
        String token = "f3l9z5o1k6";  // 渠道标识令牌



// 构建请求
        RtaRequestOuterClass.RtaRequest.Builder builder = RtaRequestOuterClass.RtaRequest.newBuilder();
        builder.setRequestId(requestId);
        builder.setChannel(channel);
        long requestTime = System.currentTimeMillis();
        builder.setRequestTime(requestTime);
        String sign = requestId + requestTime + token;
        String signKey =  DigestUtils.md5DigestAsHex(sign.getBytes()).toLowerCase();
        builder.setSign(signKey);

// 构建设备标识
        RtaRequestOuterClass.RtaRequest.Device.Builder deviceBuilder = RtaRequestOuterClass.RtaRequest.Device.newBuilder();

        String idfa = "02F004F7-1B43-4DD8-ADA0-3B5AA722BF54";
        deviceBuilder.setIdfa(idfa);
        deviceBuilder.setIdfaMd5( DigestUtils.md5DigestAsHex(idfa.getBytes()).toLowerCase());
        builder.setDevice(deviceBuilder.build());
        String promotionType = "kwai_xinhui";
        builder.addPromotionType(promotionType);

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost request = new HttpPost(url);
        request.addHeader("content-type", "application/x-protobuf");
        request.setEntity(new ByteArrayEntity(builder.build().toByteArray()));
        CloseableHttpResponse response = client.execute(request);
        client.close();
        System.out.println(response.getStatusLine().getStatusCode());
        HttpEntity entity = response.getEntity();
        byte[] bytes = EntityUtils.toByteArray(entity);
        RtaRequestOuterClass.RtaResponse rtaResponse = RtaRequestOuterClass.RtaResponse.parseFrom(bytes);
        String print = JsonFormat.printer().print(rtaResponse);
        System.out.println(print);
    }
}
